<template>
  <div>
    <PageTools>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="设备编号:">
          <el-input v-model="formInline.innerCode" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="Search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </PageTools>
    <el-card class="main">
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        :header-cell-style="{
          fontWeight: 'normal',
          background: '#f3f6fb',
        }"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="设备编号" prop="innerCode" />
        <el-table-column label="设备型号" prop="type.name" />
        <el-table-column
          label="详细地址"
          prop="node.addr"
          :formatter="addrType"
        />
        <el-table-column label="运营状态" prop="vmStatus">
          <template #default="{ row }">
            {{ vmStatusList[+row.vmStatus] }}
          </template>
        </el-table-column>
        <el-table-column label="设备状态" prop="status" width="220">
          <template #default="{ row }">
            <div class="statusBox">
              <span v-if="row.status!=='10001'">离线</span>
              <span v-if="row.status==='10002'">货道</span>
              <span v-if="row.status==='10003'">传动轴</span>
              <p v-if="row.status==='10001'">离线</p>
              <p v-if="row.status!=='10002'">货道</p>
              <p v-if="row.status!=='10003'">传动轴</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="detailsFn(row)"
            >查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row v-if="totalPage!==1" type="flex" justify="space-between" align="middle">
        <el-col :span="19">
          <p class="paginationHeader">
            {{ `共${totalCount}条记录 第${page.pageIndex}/${totalPage}页` }}
          </p>
        </el-col>
        <el-col :span="5">
          <el-pagination
            background
            :current-page="page.pageIndex"
            :page-size="page.pageSize"
            :total="totalCount"
            layout="prev, next"
            prev-text="上一页"
            next-text="下一页"
            @current-change="changePage"
          />
        </el-col>
      </el-row>
    </el-card>
    <DetailStatus :status-obj="statusObj" :dialog-status="dialogStatus" @closeStatus="closeStatus" />
  </div>
</template>

<script>
import DetailStatus from './component/detail-status.vue'
import { VmSearch } from '@/api/vm'
export default {
  components: {
    DetailStatus
  },
  data() {
    return {
      formInline: { innerCode: '' },
      loading: false,
      list: [],
      page: {
        pageIndex: 1,
        pageSize: 10
      },
      totalCount: 0,
      totalPage: 0,
      // 设备状态数字转化成文字
      vmStatusList: {
        0: '未投放',
        1: '运营',
        3: '撤机'
      },
      // 查看详情弹出框
      dialogStatus: false,
      statusObj: {}
    }
  },
  created() {
    this.VmSearch()
  },
  methods: {
    // 设备管理表格数据
    async VmSearch(innerCode) {
      this.loading = true
      const { data } = await VmSearch({ ...this.page, ...innerCode })
      this.list = data.currentPageRecords
      this.loading = false
      this.totalCount = Number(data.totalCount)
      this.totalPage = Number(data.totalPage)
    },
    // 1.查询
    Search() {
      this.VmSearch(this.formInline)
    },
    // 2.新增
    addForm() {},
    // 3.分页
    changePage(val) {
      this.page.pageIndex = val
    },
    // 4.查看详情
    detailsFn(row) {
      this.statusObj = row
      this.dialogStatus = true
    },
    // 4.1.关闭弹出层
    closeStatus() {
      this.dialogStatus = false
    },
    // 5.地址是字符串取到最后一位
    addrType(row, column, callValue, index) {
      const arr = callValue.split('-')
      const str = arr[arr.length - 1]
      return str
    }
  }
}
</script>

<style lang="scss">
.statusBox {
  span {
    display: inline-block;
    color: #fff;
    height: 23px;
    line-height: 23px;
    border-radius: 14px;
    padding: 0 12px;
    margin-left: 5px;
    background: #ff665f linear-gradient(135deg,#ffb043,#ffc020)
  }
  p {
    display: inline-block;
    color: #fff;
    height: 23px;
    line-height: 23px;
    border-radius: 14px;
    padding: 0 12px;
    margin-left: 5px;
   background: #ff665f linear-gradient(135deg,#3fc997,#4bda98);
  }
}
@import "~@/styles/mainTop.scss";
@import "~@/styles/mainAdd.scss";
@import "~@/styles/mainTable.scss";
</style>
